package com.xd.core.api.controller;


import com.alibaba.fastjson.JSONObject;
import com.xd.common.ascept.ResponseNotIntercept;
import com.xd.common.util.AESUtil;
import com.xd.core.api.dto.QMHD;
import com.xd.core.api.service.ApiContractService;
import com.xd.core.api.service.ApiService;
import com.xd.core.api.service.DLApiService;
import com.xd.core.company.entity.CompanyContract;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/notify")
public class ApiNotifyController {
    @Autowired
    private ApiService apiService;
    @Autowired
    private DLApiService dlApiService;
    @Resource
    private ApiContractService apiContractService;

    @ResponseNotIntercept
    @ApiOperation("机构注册预存款电子账户回调")
    @PostMapping("/prestoreRegisterResult")
    public HashMap<String, String> prestoreRegisterResult(String app_id,
                                                          String sign_type,
                                                          String client_trans_id,
                                                          String trans_timestamp,
                                                          String trans_type,
                                                          String callback_url,
                                                          String version,
                                                          String data,
                                                          String sign) throws Exception {
        apiService.prestoreRegisterResult(app_id, sign_type, client_trans_id, trans_timestamp, trans_type, callback_url, version, data, sign);
        HashMap<String, String> map = new HashMap<>();
        map.put("resp_code", "00000000");
        map.put("resp_msg", "回调成功");
        return map;
    }

    @ResponseNotIntercept
    @ApiOperation("放款调拨提现回调")
    @PostMapping("/outAmountAllotReturn")
    public HashMap<String, String> outAmountAllotReturn(String app_id,
                                                        String sign_type,
                                                        String client_trans_id,
                                                        String trans_timestamp,
                                                        String trans_type,
                                                        String callback_url,
                                                        String version,
                                                        String data,
                                                        String sign) throws Exception {
        apiService.outAmountAllotReturn(app_id, sign_type, client_trans_id, trans_timestamp, trans_type, callback_url, version, data, sign);
        HashMap<String, String> map = new HashMap<>();
        map.put("resp_code", "00000000");
        map.put("resp_msg", "回调成功");
        return map;
    }

    @ResponseNotIntercept
    @ApiOperation("调拨提现回调")
    @PostMapping("/allotReturn")
    public HashMap<String, String> allotReturn(String app_id,
                                               String sign_type,
                                               String client_trans_id,
                                               String trans_timestamp,
                                               String trans_type,
                                               String callback_url,
                                               String version,
                                               String data,
                                               String sign) throws Exception {
        apiService.allotReturn(app_id, sign_type, client_trans_id, trans_timestamp, trans_type, callback_url, version, data, sign);
        HashMap<String, String> map = new HashMap<>();
        map.put("resp_code", "00000000");
        map.put("resp_msg", "回调成功");
        return map;
    }

    @ResponseNotIntercept
    @ApiOperation("提现回调")
    @PostMapping("/withdrawDepositReturn")
    public HashMap<String, String> withdrawDepositReturn(String app_id,
                                                         String sign_type,
                                                         String client_trans_id,
                                                         String trans_timestamp,
                                                         String trans_type,
                                                         String callback_url,
                                                         String version,
                                                         String data,
                                                         String sign) throws Exception {
        apiService.withdrawDepositReturn(app_id, sign_type, client_trans_id, trans_timestamp, trans_type, callback_url, version, data, sign);
        HashMap<String, String> map = new HashMap<>();
        map.put("resp_code", "00000000");
        map.put("resp_msg", "回调成功");
        return map;
    }

    @ResponseNotIntercept
    @ApiOperation("电子转账回调")
    @PostMapping("/transferOrderReturn")
    public HashMap<String, String> getTransferOrderReturn(String app_id,
                                                          String sign_type,
                                                          String client_trans_id,
                                                          String trans_timestamp,
                                                          String trans_type,
                                                          String callback_url,
                                                          String version,
                                                          String data,
                                                          String sign) throws Exception {
        apiService.getTransferOrderReturn(app_id, sign_type, client_trans_id, trans_timestamp, trans_type, callback_url, version, data, sign);
        HashMap<String, String> map = new HashMap<>();
        map.put("resp_code", "00000000");
        map.put("resp_msg", "回调成功");
        return map;
    }

    @ResponseNotIntercept
    @ApiOperation("华通入金通知回调(线下对公打款到电子账户)")
    @PostMapping("/HTRevenueAccountReturn")
    public HashMap<String, String> HTRevenueAccountReturn(String app_id,
                                                          String sign_type,
                                                          String client_trans_id,
                                                          String trans_timestamp,
                                                          String trans_type,
                                                          String callback_url,
                                                          String version,
                                                          String data,
                                                          String sign) throws Exception {
        apiService.HTRevenueAccountReturn(app_id, sign_type, client_trans_id, trans_timestamp, trans_type, callback_url, version, data, sign);
        HashMap<String, String> map = new HashMap<>();
        map.put("resp_code", "00000000");
        map.put("resp_msg", "回调成功");
        return map;
    }


    //线下给宝付
    //渠道一
    @ResponseNotIntercept
    @ApiOperation("代付回调（放款）")
    @PostMapping("/outAmountReturn")
    public String outAmountReturn(@RequestParam Map<String, Object> fpnre) throws IOException {
        dlApiService.outAmountReturn(fpnre);
        return "OK";
    }

    //线下给宝付
    // 渠道二
    @ResponseNotIntercept
    @ApiOperation("渠道二代付回调（放款/提现）")
    @PostMapping("/towBaofuwithdrawDepositNotify")
    public String towBaofuwithdrawDepositNotify(@RequestParam Map<String, Object> fpnre) throws IOException {
        dlApiService.towBaofuwithdrawDepositNotify(fpnre);
        return "OK";
    }


    //渠道一  渠道二
    @ResponseNotIntercept
    @ApiOperation("直接支付(扣款回调)")
    @PostMapping("/singlePayReturn")
    public String singlePayReturn(HttpServletRequest request) throws Exception {
        dlApiService.singlePayReturn(request);
        return "OK";
    }

    //渠道一  渠道二
    @ResponseNotIntercept
    @ApiOperation("保单贷预付款-直接支付(扣款回调)")
    @PostMapping("/yfkSinglePayReturn")
    public String baodanSinglePayReturn(HttpServletRequest request) throws Exception {
        dlApiService.baodanSinglePayReturn(request);
        return "OK";
    }

    //分账异步通知
    //渠道一（渠道二不分账）
    @ResponseNotIntercept
    @ApiOperation("直接支付(分账异步通知)")
    @PostMapping("/sharenotify")
    public String ShareNotify(@RequestParam Map<String, String> sharerec) throws Exception {
        dlApiService.ShareNotify(sharerec);
        return "OK";
    }

    //渠道一
    @ResponseNotIntercept
    @ApiOperation("保单贷预付款-直接支付(分账异步通知)")
    @PostMapping("/yfkSharenotify")
    public String baodanSharenotify(@RequestParam Map<String, String> sharerec) throws Exception {
        dlApiService.baodanSharenotify(sharerec);
        return "OK";
    }

    //渠道一  渠道二
    @ResponseNotIntercept
    @ApiOperation("线下转账回调")
    @PostMapping("/offlinePayNotify")
    public String offlinePayNotify(@RequestParam Map<String, Object> fpnre) throws IOException, InterruptedException {
        dlApiService.offlinePayNotify(fpnre);
        return "OK";
    }

    //渠道一
    @ResponseNotIntercept
    @ApiOperation("线下转账分账回调")
    @PostMapping("/offlineSplitNotify")
    public String offlineSplitNotify(@RequestParam Map<String, Object> fpnre) throws IOException {
        dlApiService.offlineSplitNotify(fpnre);
        return "OK";
    }


    //合同签署完毕回调
    //借款人合同回调
    @ResponseNotIntercept
    @PostMapping("/contractNotifyUrl")
    public String contractNotifyUrl(QMHD qmhd) throws InterruptedException {
        log.info("执行借款人合同回调");
        apiContractService.contractNotifyUrl(qmhd);
        log.info("合同下载完成");
        return "ok";
    }

    //合同签署失败回调
    //借款人合同回调
    @ResponseNotIntercept
    @PostMapping("/contractCallbackUrl")
    public String contractCallbackUrl(QMHD qmhd) {
        apiContractService.contractCallbackUrl(qmhd);
        return "ok";
    }

    //合同签署完毕回调
    //公司合同回调
    @ResponseNotIntercept
    @PostMapping("/companyContractNotifyUrl")
    public String companyContractNotifyUrl(QMHD qmhd) throws InterruptedException {
        log.info("执行公司合同签署回调");
        apiContractService.companyContractNotifyUrl(qmhd);
        log.info("合同下载完成");
        return "ok";
    }

    //合同签署失败回调
    //公司合同回调
    @ResponseNotIntercept
    @PostMapping("/companyContractCallback")
    public String companyContractCallback(QMHD qmhd) {
        log.info("执行公司合同签署失败回调");
        apiContractService.companyContractCallback(qmhd);
        return "ok";
    }

    //公证流程拒绝受理
    @ResponseNotIntercept
    @PostMapping("/refuseCertificationNotifyUrl")
    public HashMap<String, Object> refuseCertification(@RequestBody JSONObject json) {
        log.info("公证流程拒绝受理");
        apiService.refuseCertification(json);
        HashMap<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "成功");
        return map;
    }
    //接收公证处返回的合同
    @ResponseNotIntercept
    @PostMapping("/acceptContractNotifyUrl")
    public HashMap<String, Object> acceptContractNotifyUrl(@RequestBody JSONObject json) {
        log.info("接收公证处返回的合同");
        apiService.acceptContractNotifyUrl(json);
        HashMap<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "成功");
        return map;
    }
    //接收公证处返回的公证书
    @ResponseNotIntercept
    @PostMapping("/acceptNotarization")
    public HashMap<String, Object> acceptNotarization(@RequestBody JSONObject json) {
        log.info("接收公证处返回的公证书");
        apiService.acceptNotarization(json);
        HashMap<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "成功");
        return map;
    }
    //接收公证处返回重新签署原因
    @ResponseNotIntercept
    @PostMapping("/signFailReason")
    public HashMap<String, Object> signFailReason(@RequestBody JSONObject json) {
        log.info("接收公证处返回重新签署原因");
        apiService.signFailReason(json);
        HashMap<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "成功");
        return map;
    }

}
